package com.rsk.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rsk.dao.extend.SysRoleExtend;
import com.rsk.utils.algorithm.RecursionAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.SysRoleMenuRepository;
import com.rsk.dao.SysRoleRepository;
import com.rsk.dao.extend.SysRoleMenuExtend;
import com.rsk.dao.utils.BatchDataUitl;
import com.rsk.dao.utils.SearchDataUtils;
import com.rsk.entity.SysRole;
import com.rsk.entity.SysRoleAuth;
import com.rsk.entity.SysRoleMenu;
import com.rsk.service.SysRoleService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;
import com.rsk.utils.tools.PageUtils;


@Service(interfaceClass = SysRoleService.class)
public class SysRoleServiceImpl implements SysRoleService{
	
	@Autowired
	private SysRoleRepository sysRoleRrepository;
	
	@Autowired
	private SysRoleMenuRepository sysRoleMenuRrepository;
	
	@Autowired
	private BatchDataUitl batchDataUitl;
	
	@Autowired
	private SysRoleMenuExtend sysRoleMenuExtend;
	
	@Autowired
	private SearchDataUtils searchDataUtils;

	@Autowired
	private SysRoleExtend sysRoleExtend;

	//角色保存
	@Transactional
	public int saveSysRole(String roleName, String roleCode, Long[] menuIds, Long[] authIds, Long operatorUser) {

		Long roleId = SnowflakeIdWorker.generateId(true);
		SysRole sysRole = new SysRole();
		sysRole.setId(roleId);
		sysRole.setRoleName(roleName);
		sysRole.setRoleCode(roleCode);
		sysRole.setOperatorUser(operatorUser);
		sysRole.setOperatorTime(new Date());
		sysRoleRrepository.saveAndFlush(sysRole);
		
		if(menuIds.length>0){
			List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
			for (int i = 0; i < menuIds.length; i++) {
				SysRoleMenu sysRoleMenu = new SysRoleMenu();
				sysRoleMenu.setId(SnowflakeIdWorker.generateId(true));
				sysRoleMenu.setRoleId(roleId);
				sysRoleMenu.setMenuId(menuIds[i]);
				sysRoleMenu.setOperatorUser(operatorUser);
				sysRoleMenu.setOperatorTime(new Date());
				list.add(sysRoleMenu);
			}
			//String sql = "INSERT INTO sys_role_menu(id,role_id,menu_id,operator_user,operator_time) VALUES (:Id,:roleId,:menuId,:operatorUser,:operatorTime)";
			batchDataUitl.batchDataName(sysRoleMenuExtend.insertSql(), list);
		}
		
		if(authIds.length>0){
			List<SysRoleAuth> list = new ArrayList<SysRoleAuth>();
			for (int i = 0; i < menuIds.length; i++) {
				SysRoleAuth sysRoleAuth = new SysRoleAuth();
				sysRoleAuth.setId(SnowflakeIdWorker.generateId(true));
				sysRoleAuth.setRoleId(roleId);
				sysRoleAuth.setAuthId(authIds[i]);
				sysRoleAuth.setOperatorUser(operatorUser);
				sysRoleAuth.setOperatorTime(new Date());
				list.add(sysRoleAuth);
			}
			String sql = "INSERT INTO sys_role_auth(id,role_id,auth_id,operator_user,operator_time) VALUES (:Id,:roleId,:authId,:operatorUser,:operatorTime)";
			batchDataUitl.batchDataName(sql, list);
		}
		
		return 1;
	}

	//角色更新
	@Transactional
	public int updateSysRole(Long id, String roleName, String roleCode,
			Long operatorUser) {
		return sysRoleRrepository.updateSysRole(id, roleName, roleCode, operatorUser);
	}

	//角色删除
	@Transactional
	public int delSysRole(Long id, Long operatorUser) {
		return sysRoleRrepository.delSysRole(id, operatorUser);
	}

	//角色查询
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	public List<Map> selectSysRole(String roleName, String roleCode) {

		Map<String, Object> map = sysRoleExtend.selectMenus(roleName, roleCode);
		String sql = map.get("sql").toString();
		Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
		List<Map> list = searchDataUtils.search(sql, paramMap, Map.class);
		list = RecursionAlgorithm.menuList(list, "id", "p_id");
		return list;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	public PageUtils<Map> selectByPage(String account, Integer nowPage, Integer pageSize) {
        Map<String, Object> map = sysRoleMenuExtend.selectByPage(account);
        String sql = map.get("sql").toString();
        String countSQL =  map.get("countSQL").toString();
        Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
        PageUtils<Map> page = searchDataUtils.searchByPage(nowPage, pageSize, sql, countSQL, paramMap, Map.class);
		return page;
	}
}
